import threading
import time
from requests_html import HTMLSession
import pyperclip
import requests

# 文件下载
def downPic(url, i):
    res = requests.get(url)
    with open(f'{i}.png', 'wb') as f:
        f.write(res.content)

# 多线程
class MultiThread(threading.Thread):
    def __init__(self, urls, len, n, pre, name, flag):
        threading.Thread.__init__(self)
        self.urls = urls
        self.len = len
        self.n = n
        self.pre = pre
        self.name = name
        self.flag = flag

    def run(self):
        for i in range((self.len / self.n) * self.flag, (self.len / self.n) * (self.flag + 1)):
            print(i)
            # if self.urls[i] != "":
            #     print(i)
            # 文件下载self.cp_tuple.

# 爬虫
def parse(url):
    session = HTMLSession()
    page = session.get(url)
    page.encoding = 'utf-8'
    content = page.html.find('ul.chapter', first=True)
    a_s = content.find('a')
    return a_s


# pre保存路径前缀
pre = r'E:\ '
# dir文件名---或多章节
name = 'xxx'
# 线程号
flag = ''
# 线程数
n = 4
# 爬取的数据
cp_tuple = ''

if __name__ == "__main__":
    url = pyperclip.paste()
    # 开启爬虫
    urls = parse(url)
    u_len = urls.length
    for i in range(n):
        thread = MultiThread(urls, u_len, n, pre, name, flag)
        thread.start()
    #     线程多余部分
    for j in range(u_len % n):
        print()
